package net.caixiaomi.info.ui.football.detail;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mobstat.StatService;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.MatchDetailOddsAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.interfaces.ComonFragmentImpl;
import net.caixiaomi.info.model.MatchIntroduceModel;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OddsFragment extends BaseFragment implements ComonFragmentImpl, View.OnClickListener {

    @BindView(android.R.id.list)
    RecyclerView mListView;
    @BindView(net.caixiaomi.info.R.id.progress)
    CircularProgressBar mProgress;
    private TextView mWin;
    private TextView mDraw;
    private TextView mDefeat;

    private TextView mBtnEurope;
    private TextView mBtnAsia;
    private TextView mBtnSize;

    private MatchDetailOddsAdapter mAdapter;
    private MatchIntroduceModel mData;

    @Override
    protected int fragmentLayout() {
        return R.layout.frag_list;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        mProgress.setVisibility(View.GONE);
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));

        View headerView = LayoutInflater.from(CommonApp.getInstance()).inflate(R.layout.header_analysis_odds, null);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = (int) getResources().getDimension(R.dimen.padding_8);
        headerView.setLayoutParams(layoutParams);
        mWin = (TextView) headerView.findViewById(R.id.win);
        mDraw = (TextView) headerView.findViewById(R.id.draw);
        mDefeat = (TextView) headerView.findViewById(R.id.defeat);
        headerView.findViewById(R.id.line).setVisibility(View.GONE);
        mBtnEurope = (TextView) headerView.findViewById(R.id.btn_europe);
        mBtnEurope.setOnClickListener(this);
        mBtnAsia = (TextView) headerView.findViewById(R.id.btn_asia);
        mBtnAsia.setOnClickListener(this);
        mBtnSize = (TextView) headerView.findViewById(R.id.btn_size);
        mBtnSize.setOnClickListener(this);

        mAdapter = new MatchDetailOddsAdapter(null);
        mAdapter.addHeaderView(headerView);
        mListView.setAdapter(mAdapter);
    }


    @Override
    public void onResume() {
        super.onResume();
        StatService.onPageStart(getActivity(), "peilvpagepv");
    }

    @Override
    public void onPause() {
        super.onPause();
        StatService.onPageEnd(getActivity(), "peilvpagepv");
    }

    public void setData(MatchIntroduceModel data) {
        mData = data;
        updateTab(mBtnEurope);
        updateDisplayData();
    }

    @Override
    public void setType(int type) {

    }

    @Override
    public void setFilter(String filter) {

    }

    @Override
    public void onClick(View v) {
        updateTab(v);
        updateDisplayData();
    }

    private void updateTab(View view) {
        if (view.isSelected())
            return;
        switch (view.getId()) {
            case R.id.btn_europe:
                mBtnAsia.setSelected(false);
                mBtnSize.setSelected(false);
                break;
            case R.id.btn_asia:
                mBtnEurope.setSelected(false);
                mBtnSize.setSelected(false);
                break;
            case R.id.btn_size:
                mBtnEurope.setSelected(false);
                mBtnAsia.setSelected(false);
                break;
        }
        view.setSelected(!view.isSelected());
        String win = "";
        String draw = "";
        String defeat = "";

        if (mBtnAsia.isSelected()) {
            win = String.valueOf(getString(R.string.C_HOST_WATER));
            draw = getString(net.caixiaomi.info.R.string.C_PLATE);
            defeat = getString(R.string.C_GUEST_WATER);
        }
        if (mBtnEurope.isSelected()) {
            win = getString(net.caixiaomi.info.R.string.C_WIN);
            draw = getString(net.caixiaomi.info.R.string.C_DRAW);
            defeat = getString(net.caixiaomi.info.R.string.C_DEFEAT);
        }
        if (mBtnSize.isSelected()) {
            win = getString(net.caixiaomi.info.R.string.C_DX_LARGE);
            draw = getString(net.caixiaomi.info.R.string.C_PLATE);
            defeat = getString(R.string.C_DX_SMALL);
        }
        mWin.setText(win);
        mDraw.setText(draw);
        mDefeat.setText(defeat);
    }


    private void updateDisplayData() {
        try {
            List<MultiItemEntity> data = new ArrayList<>();
            if (mBtnAsia.isSelected()) {
                data.addAll(mData.getLeagueMatchAsias());
            }
            if (mBtnEurope.isSelected()) {
                data.addAll(mData.getLeagueMatchEuropes());
            }
            if (mBtnSize.isSelected()) {
                data.addAll(mData.getLeagueMatchDaoxiaos());
            }
            mAdapter.setNewData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
